package com.longrise.android.byjk.plugins.poster.posterdetail;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.longrise.android.byjk.R;
import com.longrise.android.byjk.event.PosterEvent;
import com.longrise.common.base.BaseFragment;
import com.longrise.common.utils.PrintLog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;


/**
 * Created by godliness on 2017/9/23.
 * 海报详情的图片
 */

public class PosterFragment extends BaseFragment<PosterFragPresenter> implements PosterFragView {

    public static final String TAG = "PosterFragment";

    public static final String EXTRA_PARAMS_URL = "extra_params_url";
    public static final String EXTRA_PARAMS_POSITION = "extra_params_position";

    private ImageView mIvPoster;
    /***当前海报*/
    private Drawable mCurDrawable;

    private String mUrl = "";
    private int mCurrentFragment;
    private ProgressBar pb_loading;

    @Override
    protected int getLayoutResourse(@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_postershow;
    }

    @Override
    public void initView() {
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mIvPoster = (ImageView) getView().findViewById(R.id.iv_poster_share);
        pb_loading = (ProgressBar) getView().findViewById(R.id.pb_loading);
        pb_loading.setVisibility(View.VISIBLE);
        getExtraData();

        mPresenter.preformLoadImage(mUrl, mCurrentFragment, mIvPoster);
    }

    private void getExtraData() {
        Bundle args = getArguments();
        mUrl = args.getString(EXTRA_PARAMS_URL);
        mCurrentFragment = args.getInt(EXTRA_PARAMS_POSITION);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            EventBus.getDefault().register(this);
        } else {
            EventBus.getDefault().unregister(this);
        }
    }


    /**
     * 通知fragment 将图片保存到本地
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void registPostEvent(final PosterEvent event) {
        if (event.isSavePic()) {
            Disposable subscribe = Observable.create(new ObservableOnSubscribe<PosterFragPresenter.CreateSave>() {
                @Override
                public void subscribe(ObservableEmitter<PosterFragPresenter.CreateSave> e) throws Exception {
                    PosterFragPresenter.CreateSave createSave = mPresenter.preformShare(mCurDrawable);
                    e.onNext(createSave);
                }
            }).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<PosterFragPresenter.CreateSave>() {
                        @Override
                        public void accept(PosterFragPresenter.CreateSave b) throws Exception {
                            if (b != null) {
                                event.setSavePic(false);
                                event.setCreateSave(b);
                                EventBus.getDefault().post(event);
                            }
                        }
                    });
            mRxmanager.addDisposable(subscribe);
        }
    }


    @Override
    public void showToast(String msg) {
        super.showToast(msg);
    }

    @Override
    public void showLoadingDialog() {
    }

    @Override
    public void dismissLoadingDialog() {
        pb_loading.setVisibility(View.GONE);
    }

    @Override
    public void setCurrentDrawable(Drawable drawable) {
        mCurDrawable = drawable;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }
}
